package com.sbtr.util;

import com.alibaba.excel.EasyExcel;
import com.sbtr.common.file.utils.AbstractImportInterface;
import com.sbtr.common.file.utils.AbstractNoModelDataListener;
import org.mockito.internal.invocation.AbstractAwareMethod;
import org.springframework.util.StringUtils;

import java.io.File;
import java.io.FileInputStream;
import java.io.InputStream;
import java.util.*;
import java.util.stream.Collectors;

public class EasyExcelReader implements AbstractImportInterface {

//	private static Sheet initSheet;
//
//	static {
//		initSheet = new Sheet(1, 0);
//		initSheet.setSheetName("sheet");
//		// 设置自适应宽度
//		initSheet.setAutoWidth(Boolean.TRUE);
//	}

	/**
	 * 更新为3.0版本，Excel的字段名称和数据是单独分离的，需要合并到一起
	 * 读大于1000行数据
	 * 
	 * @param filePath
	 *            文件觉得路径
	 * @return
	 */
	public List<String[]> readLessThan1000Row(String filePath) {
		File file = new File(filePath);
		AbstractNoModelDataListener abstractNoModelDataListener = new AbstractNoModelDataListener(this, new HashMap<>());
		EasyExcel.read(file, abstractNoModelDataListener).sheet().doRead();
		List<String> title = abstractNoModelDataListener.titleList;
		String[] tArray = ArrayHelper.listToArray(title, String.class);
		List<String[]> result = abstractNoModelDataListener.getListStr();
		result.add(0, tArray);
		return result;
//		return abstractNoModelDataListener.getListStr();
//		return readLessThan1000RowBySheet(filePath, null);
	}

	@Override
	public List<LinkedHashMap<Integer, String>> analyze(Map<String, String> params, List<LinkedHashMap<Integer, String>> data) {
		return data;
	}

//	public static List<String[]> readLessThan1000RowBySheet(String filePath, Sheet sheet) {
//		if (!StringUtils.hasText(filePath)) {
//			return null;
//		}
//
//		sheet = sheet != null ? sheet : initSheet;
//
//		try (InputStream fileStream = new FileInputStream(filePath)) {
//			List<Object> dataInfo = EasyExcelFactory.read(fileStream, sheet);
//			List<String[]> message = dataInfo.stream().map(x -> {
//				List<String> mx = (ArrayList) x;
//				return mx.toArray(new String[mx.size()]);
//			}).collect(Collectors.toList());
//			return message;
//		} catch (Exception e) {
//			e.printStackTrace();
//		}
//		return null;
//	}

}
